package com.yangfan.thread.thread_wangwenjun06;

/**
 * 观察者的 可执行的  任务类
 *
 * 抽象的是因为不进行实现 run 方法
 *
 */
public abstract class ObservableRunnable implements Runnable {

    //观察者  监听器
    protected final LifeCycleListener listener;

    public ObservableRunnable(final LifeCycleListener listener) {
        this.listener = listener;
    }


    /**
     * 执行我们这个任务 的线程去 调用这个方法 进行通知
     */
    protected void notifyChange(final RunnableEvent event) {
        listener.onEvent(event);
    }


    //运行状态
    public enum RunnableState {
        RUNNING, ERROR, DONE
    }

    /**
     * 事件对象
     */
    public static class RunnableEvent {
        private final RunnableState state;
        private final Thread thread;
        private final Throwable cause;

        public RunnableEvent(RunnableState state, Thread thread, Throwable cause) {
            this.state = state;
            this.thread = thread;
            this.cause = cause;
        }

        public RunnableState getState() {
            return state;
        }

        public Thread getThread() {
            return thread;
        }

        public Throwable getCause() {
            return cause;
        }
    }
}
